package com.mc.lp.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.mc.lp.database.dao.IRecommendationDAO;
import com.mc.lp.database.entity.RecommendationEntity;
import com.mc.lp.database.entity.helper.RecommendationEntityHelper;
import com.mc.lp.database.exception.ConcurrentAccessException;
import com.mc.lp.database.exception.DataNotFoundException;
import com.mc.lp.service.MicroCampusAbstractService;
import com.mc.lp.service.RecommendationService;
import com.mc.lp.shared.vo.IValueObject;
import com.mc.lp.shared.vo.RecommendationVO;
import com.mc.lp.shared.vo.SearchCriteriaVO;
/*
 * @author aphanda
 * @date Dec 13, 2014
 * @time 9:15:03 PM
 */


@Service
public class RecommendationServiceImpl extends MicroCampusAbstractService<RecommendationServiceImpl> implements RecommendationService {
	
	@Resource
	IRecommendationDAO recommendationDAO;
	@Resource
	RecommendationEntityHelper helper;
	// DV Please also implement the Recommendation Entity helper class to convert object to VO
	@Transactional
	public IValueObject saveRecommendations(RecommendationVO recommendationVO)
			throws DataNotFoundException{
		RecommendationEntityHelper helper = new RecommendationEntityHelper();
		RecommendationEntity entity = new RecommendationEntity();
		try {
			if (recommendationVO.getId() != null){
				entity = recommendationDAO.findById(recommendationVO.getId());
			}
			//this is important to set only variable fields if entity is get by database call to retain id and other hibernate related stuff
			helper.setEntity(recommendationVO,entity);
			
			RecommendationEntity savedEntity=recommendationDAO.saveOrUpdate(entity);
			recommendationVO.setId(savedEntity.getId());//this is important to return id back to client
		} catch (DataAccessException e) {
			throw new DataNotFoundException("Unable to save recommendations");
		}catch (ConcurrentAccessException e) {
			throw new DataNotFoundException("Unable to save recommendations");
		}
		return recommendationVO;
	}
	
	@Transactional
	public List<RecommendationVO> getRecommendationBySearchCriteria(SearchCriteriaVO crit) throws DataNotFoundException {
		List<RecommendationVO> recommendations = new ArrayList<RecommendationVO>();
		try {
			List<RecommendationEntity> list = recommendationDAO.fetchRecommendationBySearchCriteria(crit);
			for (RecommendationEntity recommendationEntity : list) {
				//user helper to parse entity
				RecommendationVO vo = helper.getVO(recommendationEntity);
				recommendations.add(vo);
			}
		} catch (DataAccessException e) {
			throw new DataNotFoundException("Unable to find recomendation by record Type");
		}
		return recommendations;
	}

	/* (non-Javadoc)
	 * @see com.mc.lp.service.RecommendationService#deleteRecommendation(java.lang.Long)
	 */
	@Override
	@Transactional
	public boolean deleteRecommendation(Long Id) throws DataNotFoundException {
		
		try{
			if(Id !=null && Id>0){
				RecommendationEntity entity=recommendationDAO.get(Id);
				if(entity != null){
					recommendationDAO.delete(entity);
					return true;
				}else{
					throw new DataNotFoundException("No recommendation found with id:"+ Id);
				}
			}
		}catch(Exception e){
			return false;
		}
		return false;
	}
}
